package com.factory.controller;

import com.factory.domain.SocketData;
import com.factory.domain.excelData;
import com.factory.http.AjaxResult;
import com.factory.service.ISocketDataService;
import com.factory.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.factory.http.AjaxResult.success;
import static com.factory.http.AjaxResult.toAjaxResult;

/**
 * Socket原数据Controller
 * @author ruoyi
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/system/data")
public class SocketDataController {
    @Autowired
    private ISocketDataService socketDataService;

    /**
     * 根据系统查询数据
     */
    @GetMapping("/list")
    public AjaxResult list(Long systemId) {
        List<SocketData> list = socketDataService.selectSocketDataList(systemId);
        return success(list);
    }

    /**
     * 新增Socket原数据
     */
    @PostMapping("/add")
    public AjaxResult add(String Data, Long systemId) {
        Date newDateTime = DateUtils.getNewDateTime();
        SocketData socketData1 = new SocketData();
        socketData1.setTime(newDateTime);
        socketData1.setSystemId(systemId);
        socketData1.setContext(Data);
        return toAjaxResult(socketDataService.insertSocketData(socketData1));
    }

    /**
     * 查询最新的一条数据
     */
    @GetMapping("/latest")
    public AjaxResult latest(SocketData socketData) {
        SocketData socket = socketDataService.selectSocketLatestData(socketData);
        return success(socket);
    }


    //根据页码查询数据
    @GetMapping("/getListPage")
    public AjaxResult getListPage(Long systemId, Long page) {
        if (page != null) {
            Long startPage = (page - 1) * 20;
            Long endPage = page * 20;
            List<SocketData> socketData = socketDataService.socketListPage(systemId, startPage, endPage);
            for (int i = 0; i < socketData.size(); i++) {
                socketData.get(i).setId(((page - 1) * 20) + (i + 1));
            }
            return success(socketData);
        }
        return null;
    }

    //根据系统查Excel表格数据
    @GetMapping("/getExcelData")
    public AjaxResult getExcelData(Long systemId, String createTime, String endTime) {
        List<SocketData> socketData = socketDataService.socketParameterCurve(
                systemId, createTime, endTime);
        List<excelData> excelDataList = new ArrayList<>();
        for (int i = 0; i < socketData.size(); i++) {
            excelData excelData1 = new excelData();
            excelData1.setId((long) i);
            excelData1.setSystemId(systemId);
            excelData1.setTime(socketData.get(i).getTime());
            excelData1.setContext(socketData.get(i).getContext());
            excelDataList.add(excelData1);
        }
        return success(excelDataList);
    }

    //根据时间查找曲线图
    @GetMapping("/getCurve")
    public AjaxResult getCurve(Long systemId, String createTime, String endTime) {
        List<SocketData> socketData = socketDataService.socketParameterCurve(
                systemId, createTime, endTime);
        return success(getResult(socketData, systemId));
    }

    //封装曲线图数据
    private AjaxResult getResult(List<SocketData> socketData, Long systemId) {
        List<String> listOne = new ArrayList<>();
        List<String> listTwo = new ArrayList<>();
        List<String> listThree = new ArrayList<>();
        List<String> listFour = new ArrayList<>();
        List<String> listFive = new ArrayList<>();
        List<String> listSix = new ArrayList<>();
        List<String> listSeven = new ArrayList<>();
        List<String> listEight = new ArrayList<>();
        List<String> listNine = new ArrayList<>();
        List<String> listTen = new ArrayList<>();
        List<String> listEleven = new ArrayList<>();
        List<String> listTwelve = new ArrayList<>();
        List<String> listThirteen = new ArrayList<>();
        List<String> listFourteen = new ArrayList<>();
        List<String> listFifteen = new ArrayList<>();
        List<String> listSixteen = new ArrayList<>();
        List<String> listSeventeen = new ArrayList<>();
        List<String> listEighteen = new ArrayList<>();
        List<String> listNineteen = new ArrayList<>();
        List<String> listTwenty = new ArrayList<>();
        List<String> listTwentyOne = new ArrayList<>();
        List<String> listTwentyTwo = new ArrayList<>();
        List<String> listTwentyThree = new ArrayList<>();
        List<String> time = new ArrayList<>();
        //时间集合
        AjaxResult ajaxResult = new AjaxResult();
        for (SocketData socketDatum : socketData) {
            String context = socketDatum.getContext().substring(1,socketDatum.getContext().length()-1);
            List<String> result = Arrays.asList(context.split(","));
            if (result.size()>30){
                if (systemId == 1) {
                    //高压水系统
                    listOne.add(result.get(48));
                    listTwo.add(result.get(49));
                    listThree.add(result.get(50));
                    listFour.add(result.get(51));
                    listFive.add(result.get(52));
                    listSix.add(result.get(53));
                    listSeven.add(result.get(54));
                    listEight.add(result.get(55));
                    listNine.add(result.get(56));
                    listTen.add(result.get(57));
                    listEleven.add(result.get(58));
                    listTwelve.add(result.get(59));
                    listThirteen.add(result.get(60));
                    listFourteen.add(result.get(61));
                    listFifteen.add(result.get(62));
                    listSixteen.add(result.get(63));
                    listSeventeen.add(result.get(64));
                    listEighteen.add(result.get(65));
                    listNineteen.add(result.get(66));
                    listTwenty.add(result.get(67));
                    listTwentyOne.add(result.get(68));
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    String currentTime = sdf.format(socketDatum.getTime());
                    time.add(currentTime);
                }
                if (systemId == 2) {
                    //低压水系统
                    listOne.add(result.get(41));
                    listTwo.add(result.get(42));
                    listThree.add(result.get(43));
                    listFour.add(result.get(44));
                    listFive.add(result.get(45));
                    listSix.add(result.get(46));
                    listSeven.add(result.get(47));
                    listEight.add(result.get(48));
                    listNine.add(result.get(49));
                    listTen.add(result.get(50));
                    listEleven.add(result.get(51));
                    listTwelve.add(result.get(52));
                    listThirteen.add(result.get(53));
                    listFourteen.add(result.get(54));
                    listFifteen.add(result.get(55));
                    listSixteen.add(result.get(56));
                    listSeventeen.add(result.get(57));
                    listEighteen.add(result.get(58));
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    String currentTime = sdf.format(socketDatum.getTime());
                    time.add(currentTime);
                }
                if (systemId == 3) {
                    //液压系统
                    listOne.add(result.get(81));
                    listTwo.add(result.get(82));
                    listThree.add(result.get(83));
                    listFour.add(result.get(84));
                    listFive.add(result.get(85));
                    listSix.add(result.get(86));
                    listSeven.add(result.get(87));
                    listEight.add(result.get(88));
                    listNine.add(result.get(89));
                    listTen.add(result.get(90));
                    listEleven.add(result.get(91));
                    listTwelve.add(result.get(92));
                    listThirteen.add(result.get(93));
                    listFourteen.add(result.get(94));
                    listFifteen.add(result.get(95));
                    listSixteen.add(result.get(96));
                    listSeventeen.add(result.get(97));
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    String currentTime = sdf.format(socketDatum.getTime());
                    time.add(currentTime);
                }
                if (systemId == 4) {
                    //滑油系统
                    listOne.add(result.get(81));
                    listTwo.add(result.get(82));
                    listThree.add(result.get(83));
                    listFour.add(result.get(84));
                    listFive.add(result.get(85));
                    listSix.add(result.get(86));
                    listSeven.add(result.get(87));
                    listEight.add(result.get(88));
                    listNine.add(result.get(89));
                    listTen.add(result.get(90));
                    listEleven.add(result.get(91));
                    listTwelve.add(result.get(92));
                    listThirteen.add(result.get(93));
                    listFourteen.add(result.get(94));
                    listFifteen.add(result.get(95));
                    listSixteen.add(result.get(96));
                    listSeventeen.add(result.get(97));
                    listEighteen.add(result.get(98));
                    listNineteen.add(result.get(100));
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    String currentTime = sdf.format(socketDatum.getTime());
                    time.add(currentTime);
                }
                if (systemId == 5) {
                    //高压空气
                    listOne.add(result.get(15));
                    listTwo.add(result.get(16));
                    listThree.add(result.get(17));
                    listFour.add(result.get(18));
                    listFive.add(result.get(19));
                    listSix.add(result.get(20));
                    listSeven.add(result.get(21));
                    listEight.add(result.get(22));
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    String currentTime = sdf.format(socketDatum.getTime());
                    time.add(currentTime);
                }
            }
        }
        ajaxResult.put("One", listOne);
        ajaxResult.put("Two", listTwo);
        ajaxResult.put("Three", listThree);
        ajaxResult.put("Four", listFour);
        ajaxResult.put("Five", listFive);
        ajaxResult.put("Six", listSix);
        ajaxResult.put("Seven", listSeven);
        ajaxResult.put("Eight", listEight);
        ajaxResult.put("Nine", listNine);
        ajaxResult.put("Ten", listTen);
        ajaxResult.put("Eleven", listEleven);
        ajaxResult.put("Twelve", listTwelve);
        ajaxResult.put("Thirteen", listThirteen);
        ajaxResult.put("Fourteen", listFourteen);
        ajaxResult.put("Fifteen", listFifteen);
        ajaxResult.put("Sixteen", listSixteen);
        ajaxResult.put("Seventeen", listSeventeen);
        ajaxResult.put("Eighteen", listEighteen);
        ajaxResult.put("Nineteen", listNineteen);
        ajaxResult.put("Twenty", listTwenty);
        ajaxResult.put("TwentyOne", listTwentyOne);
        ajaxResult.put("TwentyTwo", listTwentyTwo);
        ajaxResult.put("TwentyThree", listTwentyThree);
        ajaxResult.put("Time", time);
        return ajaxResult;
    }
}
